package com.study.d024chart;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Side;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.stage.Stage;

/**Chart接口 ,   XYChart 子接口 的实现类 BarChart
 * BarChart 直方图的属性设置
*@description
*@author xtayhame
*date 2020年3月17日
*version
*since v1.0
*/
public class BarChartMain2 extends Application{
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane anchorPane = new AnchorPane();
		HBox hBox = new HBox(10);
		BarChart<String, Number>  barChart1 = getBarChart1();
		//BarChart<String, Number>  barChart2 = getBarChart2();
		//BarChart<Number,String >  barChart3 = getBarChart3();

		hBox.getChildren().addAll(barChart1);
		anchorPane.getChildren().add(hBox);
		stage.setScene(new Scene(anchorPane, 900, 500));
		stage.show();
	}
	public BarChart<String,Number> getBarChart1(){
		CategoryAxis x = new CategoryAxis();
		x.setLabel("国家");

		//属性设置
		//设置柱状图距离左边Y轴的距离
		x.setStartMargin(10);
		//设置柱状图距离右边的距离
		x.setEndMargin(10);
		//设置前后是否预留空间，默认true
		x.setGapStartAndEnd(true);

		//设置x轴的文件说明的位置
		x.setSide(Side.BOTTOM);

		//设置x轴上 文字说明的颜色
		x.setTickLabelFill(Paint.valueOf("#12FF12"));
		//设置x轴上 文字说明的字体大小
		x.setTickLabelFont(Font.font(15));
		//设置x上的文字说明与x轴之间的间距
		x.setTickLabelGap(2);
		//设置x上文字说明的旋转角度
		x.setTickLabelRotation(50);
		//设置垂直于x轴的线的长度
		x.setTickLength(0);

		x.setTickMarkVisible(true);


		//NumberAxis(最小值，最大值，增长值);
		NumberAxis  y = new NumberAxis(0, 100, 10);
		y.setLabel("总值");

		BarChart<String, Number> barChart = new BarChart<String, Number>(x, y);

		ObservableList<XYChart.Series<String,Number>> datas  = FXCollections.observableArrayList();


		XYChart.Series<String,Number>  xy1 = new XYChart.Series<String,Number>();
		xy1.setName("GDP");

		ObservableList<XYChart.Data<String,Number>> data1 = FXCollections.observableArrayList();
		XYChart.Data<String,Number> d1 = new XYChart.Data<String,Number>("中国",87);
		XYChart.Data<String,Number> d2= new XYChart.Data<String, Number>("美国", 76);
		XYChart.Data<String,Number> d3= new XYChart.Data<String, Number>("日本", 34);
		data1.addAll(d1,d2,d3);
		xy1.setData(data1);






		XYChart.Series<String,Number>  xy2 = new XYChart.Series<String,Number>();
		xy2.setName("GNP");
		ObservableList<XYChart.Data<String,Number>> data2 = FXCollections.observableArrayList();
		XYChart.Data<String,Number> d4= new XYChart.Data<String, Number>("中国", 12.0);
		XYChart.Data<String,Number> d5= new XYChart.Data<String, Number>("美国", 65);
		XYChart.Data<String,Number> d6= new XYChart.Data<String, Number>("日本", 32);
		data2.addAll(d4,d5,d6);
		xy2.setData(data2);

		datas.addAll(xy1,xy2);

		barChart.setData(datas);

		barChart.setPrefSize(400, 400);
		barChart.setTitle("第一种展现方式");
		return barChart;
	}

}
